﻿/**
  NELMATE   ELMATEC   LM      EL  TECONELMA  CO         NE          MA   ONELMAT    ELMATECO  LMATECONE 
 ON        NE     CO  LMA     EL  TE         CO         NEL        LMA  CO      EC     AT     LM       
CO         NE     CO  LMAT    EL  TE         CO         NELM      ELMA  CO      EC     AT     LM
CO         NE     CO  LM TE   EL  TECONELM   CO         NE MA    NE MA  CONELMATEC     AT     LMATECONE
CO         KC     CO  LM  EC  EL  TE         CO         NE  AT  ON  MA  CO      EC     AT     LM
 ON        KC     CO  LM   CO EL  TE         CO         NE   TECO   MA  CO      EC     AT     LM
  NELMATE   ELMATEC   LM    ONEL  TECONELMA  CONELMATE  NE    EC    MA  CO      EC     AT     LMATECONE
_______________________________________________________________________________________________________
 ConelMate Framework 3.0 - copyright 2007(c) Ruben Sainz de la Maza
 
	@class: 	Connect
	@vertion: 	1.0
	@date:		25/02/08
				
	@language: 	ActionScript 3.0
	@author:	Ruben Sainz de la Maza
			
**/


package com.conelmate.nc
{
	import com.conelmate.util.Trace;
	import com.conelmate.nc.NetConnManager;	
	
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.NetStatusEvent;
	import flash.net.Responder;


	public class Connect extends Trace
	{	
		static	public	const 	CONNECTED			:String = "CONNECTED";		
		static	public	const 	CLOSED				:String = "CLOSED";
		static	public	const 	FAILED				:String = "FAILED";
		static	public	const 	REJECTED			:String = "REJECTED";
		
		static internal var		_INSTANCE			:Connect;		//instancia de esta clase (singleton)
				private	var		_ARGUMENTS;							//objeto con el nick del usuario
				private	var		_objNetConnManager	:NetConnManager;
				private var		_strError			:String = "";
				private var		_strMsg				:String = "";				
		
		
//_________________________________________________________________________________ Constructor		
		public function Connect(enforcer:SingletonEnforcer)
		{
			_isTrace = false;
			mTrace("#Connect# constructor");
		}


//_________________________________________________________________________________ Public Methods

		/**
		 * singleton
		 * devolver una instancia de la clase
		 */	
		public static function getInstance():Connect
		{
			if(Connect._INSTANCE == null) Connect._INSTANCE = new Connect(new SingletonEnforcer());
			return Connect._INSTANCE;
		}
		
		
		/**
		 * inicializar
		 */		
		public function init():void
		{
			mTrace("#Connect# init");
			
			_objNetConnManager = NetConnManager.getInstance();
			_objNetConnManager.addEventListener(NetConnManager.CONNECT_SUCCESS,	 onConnect);			
			_objNetConnManager.addEventListener(NetConnManager.CONNECT_CLOSED, 	 onClosed);
			_objNetConnManager.addEventListener(NetConnManager.CONNECT_FAILED, 	 onFailed);
			_objNetConnManager.addEventListener(NetConnManager.CONNECT_REJECTED, onRejected);
			
			_objNetConnManager.arguments = _ARGUMENTS;
			_objNetConnManager.init();
		}
		
		
		/**
		 * destruir objetos y variables
		 */	
		public function destroy():void
		{
			mTrace("#Connect# destroy");
			_objNetConnManager.destroy();
			Connect._INSTANCE = null;
		}

		
//_________________________________________________________________________________ Private Methods

		/**
		 * al conectar, iniciar el administrador de pantallas
		 */		
		private function onConnect(e:Event):void
		{
			mTrace("#Connect# onConnect");			
			dispatchEvent(new Event(Connect.CONNECTED));			
		}
		

		/**
		 * al cerrarse la coneccion
		 */		
		private function onClosed(e:Event):void
		{
			mTrace("#Connect# onClosed");
			dispatchEvent(new Event(Connect.CLOSED));
		}
		
		
		/**
		 * al fallar la coneccion
		 */		
		private function onFailed(e:Event):void
		{
			mTrace("#Connect# onFailed " + e);
			dispatchEvent(new Event(Connect.FAILED));
		}
		
		
		/**
		 * al rechazar la coneccion
		 */		
		private function onRejected(e:Event):void
		{
			mTrace("#Connect# onRejected " + e);
			
			_strError = e.target.error;
			_strMsg	  = e.target.msg;			
		//	_objNetConnManager.removeEventListener(NetConnManager.CONNECT_REJECTED,onRejected);

			dispatchEvent(new Event(Connect.REJECTED));
		}		

//_________________________________________________________________________________ SETTERS

		public function set rtmp		(p:String)	{ _objNetConnManager.rtmp = p; }
		public function set ports		(p:Array)	{ _objNetConnManager.ports = p; }
		public function set instance	(p:String)	{ _objNetConnManager.instance = p; }
		public function set room		(p:String)	{ _objNetConnManager.room = p; }
		public function set arguments	(p)			{ _ARGUMENTS = p; }
		
//_________________________________________________________________________________ GETTERS		
		public function get error	():String		{ return _strError; }
		public function get msg		():String		{ return _strMsg; }			
	}
}

internal class SingletonEnforcer{}